package com.goodluck.goodluckim.ui.msg.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.goodluck.goodluckim.R;
import com.goodluck.goodluckim.db.model.CSMessage;
import com.goodluck.goodluckim.db.model.IMMessage;
import com.goodluck.goodluckim.sdk.GLIMClient;
import com.goodluck.goodluckim.sdk.actions.BaseAction;
import com.goodluck.goodluckim.sdk.actions.ImageAction;
import com.goodluck.goodluckim.sdk.actions.VideoAction;
import com.goodluck.goodluckim.sdk.constant.SessionType;
import com.goodluck.goodluckim.sdk.module.Container;
import com.goodluck.goodluckim.sdk.module.ModuleProxy;
import com.goodluck.goodluckim.sdk.module.input.InputPanel;
import com.goodluck.goodluckim.sdk.session.SessionCustomization;
import com.goodluck.goodluckim.ui.msg.MessageListPanel;
import com.goodluck.goodluckim.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;

/**
 * Created by wyj on 2018/9/10.
 */

public class MessageFragment extends Fragment implements ModuleProxy {
    private SessionCustomization customization;
    private InputPanel inputPanel;
    private MessageListPanel messageListPanel;

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        inputPanel.onActivityResult(requestCode, resultCode, data);
//        messageListPanel.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View mainView = inflater.inflate(R.layout.fragment_message, null);
        customization = (SessionCustomization) getArguments().getSerializable("customization");
        Container messageContainer = new Container(getActivity(), "", SessionType.TEAM, this);
        if (inputPanel == null) {
            inputPanel = new InputPanel(messageContainer, mainView, getActionList());
            inputPanel.setCustomization(customization);
        } else {
            inputPanel.reload(messageContainer, customization);
        }
        messageListPanel = new MessageListPanel(messageContainer, mainView);
        return mainView;
    }

    @Override
    public boolean sendMessage(CSMessage msg) {
//        JSONObject jsonObject = new JSONObject();
//        try {
//            jsonObject.put("action", MsgActions.SC_MESSAGE_RESULT);
//            JSONObject data = new JSONObject();
//            data.put("messageId", 100);
//            jsonObject.put("data", data);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        WebSocketDispatcher.getInstance().dispatcher(jsonObject.toString());
        GLIMClient.getInstance().getMessageService().sendMessage(msg).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) {
                if (future.isSuccess()) {
                    LogUtil.i("Message", "消息发送成功");
                } else {
                    LogUtil.i("Message", "消息发送失败");
                }
            }
        });
        return true;
    }

    @Override
    public void onInputPanelExpand() {
        messageListPanel.scrollToBottom();
    }

    @Override
    public void shouldCollapseInputPanel() {
        inputPanel.collapse(false);
    }

    @Override
    public boolean isLongClickEnabled() {
        return false;
    }

    @Override
    public void onItemFooterClick(IMMessage message) {

    }

    // 操作面板集合,子类可添加或者在全局设置通用面板
    protected List<BaseAction> getActionList() {
        List<BaseAction> actions = new ArrayList<>();
        actions.add(new ImageAction());
        actions.add(new VideoAction());

        if (customization != null && customization.actions != null) {
            actions.addAll(customization.actions);
        }
        return actions;
    }

    /**
     * 判断是否可以返回（软键盘、表情等是否打开状态）
     *
     * @return
     */
    public boolean onBackPressed() {
        if (inputPanel.collapse(true)) {
            return true;
        }
        if (messageListPanel.onBackPressed()) {
            return true;
        }
        return false;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (messageListPanel != null) {
            messageListPanel.onDestroy();
        }
    }
}
